<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/6
 * Time: 13:05
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\api\model\Collect as collectModel;
class Comment extends Common
{
    /*
     * 发表评论
     */
    public function addCircleComment()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //敏感词的处理
            $mingan = Db::name('mingan')->where('is_del',0)->select();
            $arr = [];
            foreach ($mingan as $k=>$v){
                array_push($arr,$v['content']);
            }
            $badword1 = array_combine($arr,array_fill(0,count($arr),'***'));
            $post['content'] = strtr($post['content'], $badword1);
            $saveData = array(
                'wid'=>$post['id'],//文章id
                'uid'=>$post['uid'],
                'content'=>$post['content'],
                'time'=>date("Y-m-d H:i:s")
            );
            if($post['type']==1){
                $res = Db::name('comment_method')->insertGetId($saveData);
            }else{
                $res = Db::name('comment_circle')->insertGetId($saveData);
            }
            if($res){
                if($post['type']==1){
                    //获取攻略的
                    $artcileInfo = Db::name('method')->field('uid')->where('mid',$post['id'])->find();
                }else{
                    $artcileInfo = Db::name('friends_circle')->field('uid')->where('f_id',$post['id'])->find();
                }
                //敏感词的处理
                $mingan = Db::name('mingan')->where('is_del',0)->select();
                $arr = [];
                foreach ($mingan as $k=>$v){
                    array_push($arr,$v['content']);
                }
                $badword1 = array_combine($arr,array_fill(0,count($arr),'***'));
                $post['content'] = strtr($post['content'], $badword1);
                //插入消息
                if($post['type']==1){
                    $messageData = [
                        'type'=> 1,
                        'wid' => $post['id'],
                        'time' =>date("Y-m-d H:i:s"),
                        'kinds'=>1,
                        'comment_content' => $post['content'],
                        'comment_uid' => $post['uid'],
                        'uid'=>$artcileInfo['uid']
                    ];
                    Db::name('comment_method_message')->insert($messageData);
                }else{
                    $messageData = [
                        'type'=> 1,
                        'wid' => $post['id'],
                        'time' =>date("Y-m-d H:i:s"),
                        'kinds'=>2,
                        'comment_content' => $post['content'],
                        'comment_uid' => $post['uid'],
                        'uid'=>$artcileInfo['uid']
                    ];
                    Db::name('comment_message')->insert($messageData);
                }

                return json(array('code'=>'200','msg'=>'评论成功'));
            }else{
                return json(array('code'=>'400','msg'=>'评论失败'));
            }
        }
    }

    //评论回复接口
    public function addCircleReplay()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //获取回复的评论id
            if($post['type']==1){
                $theCommentFind = Db::name('comment_method')->where("id",$post['pid'])->find();
            }else{
                $theCommentFind = Db::name('comment_circle')->where("id",$post['pid'])->find();
            }
            //敏感词的处理
            $mingan = Db::name('mingan')->where('is_del',0)->select();
            $arr = [];
            foreach ($mingan as $k=>$v){
                array_push($arr,$v['content']);
            }
            $badword1 = array_combine($arr,array_fill(0,count($arr),'***'));
            $post['content'] = strtr($post['content'], $badword1);
            $saveData = array(
                'wid'=>$theCommentFind['wid'],//文章id
                'uid'=>$post['uid'],//用户id
                'content'=>$post['content'],
                'time'=>date("Y-m-d H:i:s"),
                'did'=>$theCommentFind['did']?$theCommentFind['did']:$post['pid'],//楼主id
                'pid'=>$post['pid'], //父级id
                'puid'=>$theCommentFind['uid'] //父级id
            );
            if($post['type']==1){
                $res = Db::name('comment_method')->insertGetId($saveData);
            }else{
                $res = Db::name('comment_circle')->insertGetId($saveData);
            }
            if($res){
                if($post['type']==1){
                    //插入消息
                    $messageData = [
                        'type'=> 2,
                        'wid' => $theCommentFind['wid'],
                        'kinds'=>1,
                        'time' =>date("Y-m-d H:i:s"),
                        'comment_content' => $post['content'],
                        'comment_uid' => $post['uid'],
                        'self_comment' => $theCommentFind['content'],
                        'uid'=>$theCommentFind['uid']
                    ];
                    Db::name('comment_method_message')->insert($messageData);
                }else{
                    //插入消息
                    $messageData = [
                        'type'=> 2,
                        'wid' => $theCommentFind['wid'],
                        'kinds'=>2,
                        'time' =>date("Y-m-d H:i:s"),
                        'comment_content' => $post['content'],
                        'comment_uid' => $post['uid'],
                        'self_comment' => $theCommentFind['content'],
                        'uid'=>$theCommentFind['uid']
                    ];
                    Db::name('comment_message')->insert($messageData);
                }

                return json(array('code'=>'200','msg'=>'回复成功'));
            }else{
                return json(array('code'=>'400','msg'=>'回复失败'));
            }
        }
    }

    //顶级评论列表
    public function circleComment()
    {
       
        if(Request::instance()->isPost()){
                $post = Request::instance()->post();
                $page = $this->p();
                //查询文章的顶级评论
                $where['t1.pid'] = array('eq',0);
                if(empty($post['id'])){
                    return json(array('code'=>'400','msg'=>'为传入对象id'));
                }
                $where['wid'] = array('eq',$post['id']);
                $where['t1.is_display'] = array('eq',0);
                $where['t1.is_del'] = array('eq',0);
                if(empty($post['type'])){
                    return json(array('code'=>'400','msg'=>'为传入对象类型'));
                }
                if($post['type']==1){
                    $comment = Db::name('comment_method')
                        ->alias('t1')
                        ->field('t1.id,t1.content,t1.time,t1.like_num,t2.uid,t2.user_icon,t2.nickname')
                        ->join('user t2','t1.uid=t2.uid','LEFT')
                        ->where($where)
                        ->order("t1.id desc")
                        ->limit(($page-1)*config('page_size'),config('page_size'))
                        ->select();
                }else{
                    $comment =  Db::name('comment_circle')
                        ->alias('t1')
                        ->field('t1.id,t1.content,t1.time,t1.like_num,t2.uid,t2.user_icon,t2.nickname')
                        ->join('user t2','t1.uid=t2.uid','LEFT')
                        ->where($where)
                        ->order("t1.id desc")
                        ->limit(($page-1)*config('page_size'),config('page_size'))
                        ->select();
                }
                
            foreach ($comment as $key => $value){
                $comment[$key]['c_comment_num'] = 0;
                if($post['type']==1){
                    $comment[$key]['huifu'] = Db::name('comment_method')
                        ->alias('t1')
                        ->field('t1.id,t1.content,t1.time,t2.uid,t2.user_icon,t2.nickname,t3.nickname as p_nickname')
                        ->join('user t2','t1.uid=t2.uid','LEFT')
                        ->join('user t3','t3.uid=t1.puid','LEFT')
                        ->where("did",$value['id'])
                        ->where("t1.is_display",0)
                        ->where("t1.is_del",0)
                        ->order("t1.id desc")
                        ->limit(2)
                        ->select();
                }
                if($post['type']==2){
                    $comment[$key]['huifu'] =  Db::name('comment_circle')
                        ->alias('t1')
                        ->field('t1.id,t1.content,t1.time,t2.uid,t2.user_icon,t2.nickname,t3.nickname as p_nickname')
                        ->join('user t2','t1.uid=t2.uid','LEFT')
                        ->join('user t3','t3.uid=t1.puid','LEFT')
                        ->where("did",$value['id'])
                        ->where("t1.is_display",0)
                        ->where("t1.is_del",0)
                        ->order("t1.id desc")
                        ->limit(2)
                        ->select();
                }
            }
                //查询自评论
                $where2['pid'] = array('neq',0);
                $where2['wid'] = array('eq',$post['id']);
                if($post['type']==1){
                    $c_comment = Db::name('comment_method')->where($where2)->select();
                }else{
                    $c_comment = Db::name('comment_circle')->where($where2)->select();
                }

                foreach ($comment as $key => $value){
                    $comment[$key]['c_comment_num'] = 0;
                    $comment[$key]['time'] = format_date(strtotime($value['time']));
                }

                foreach ($comment as $k2 => $v2){
                    foreach ($c_comment as $k => $v){
                        if ($v2['id'] == $v['did']){
                            $comment[$k2]['c_comment_num']++;
                        }
                    }
                }


                return json(array('code'=>'200','msg'=>'请求成功','data'=>$comment));
            }


       

    }

    //回复详情
    public function replyDetail()
    {
        if (Request::instance()->isPost()){
            $post = Request::instance()->post();
            $page = $this->p();
            if($post['type']==1){
                //查询楼主信息
                $louzhu = Db::name('comment_method')
                    ->alias('t1')
                    ->field('t1.id,t1.content,t1.time,t2.uid,t2.user_icon,t2.nickname')
                    ->join('user t2','t1.uid=t2.uid','LEFT')
                    ->where("id",$post['id'])
                    ->find();

                //查询全部回复
                $huifu = Db::name('comment_method')
                    ->alias('t1')
                    ->field('t1.id,t1.content,t1.time,t2.uid,t2.user_icon,t2.nickname,t3.nickname as p_nickname')
                    ->join('user t2','t1.uid=t2.uid','LEFT')
                    ->join('user t3','t3.uid=t1.puid','LEFT')
                    ->where("did",$post['id'])
                    ->where("t1.is_display",0)
                    ->where("t1.is_del",0)
                    ->order("t1.id desc")
                    ->limit(($page-1)*config('page_size'),config('page_size'))
                    ->select();
            }else{
                //查询楼主信息
                $louzhu = Db::name('comment_circle')
                    ->alias('t1')
                    ->field('t1.id,t1.content,t1.time,t1.like_num,t2.uid,t2.user_icon,t2.nickname')
                    ->join('user t2','t1.uid=t2.uid','LEFT')
                    ->where("id",$post['id'])
                    ->find();

                //查询全部回复
                $huifu = Db::name('comment_circle')
                    ->alias('t1')
                    ->field('t1.id,t1.content,t1.time,t2.uid,t2.user_icon,t2.nickname,t3.nickname as p_nickname')
                    ->join('user t2','t1.uid=t2.uid','LEFT')
                    ->join('user t3','t3.uid=t1.puid','LEFT')
                    ->where("did",$post['id'])
                    ->where("t1.is_display",0)
                    ->where("t1.is_del",0)
                    ->order("t1.id desc")
                    ->limit(($page-1)*config('page_size'),config('page_size'))
                    ->select();
            }


            foreach ($huifu as $key => $value){
                if (!$value['p_nickname']){
                    $huifu[$key]['p_nickname'] = '';
                }
                $huifu[$key]['time'] = format_date(strtotime($value['time'])); 
            }
 
                   $louzhu['time'] = format_date(strtotime($louzhu['time'])); 
                 

            return json(array('code'=>'200','msg'=>'请求成功','data'=>array('louzhu'=>$louzhu,'huifu'=>$huifu)));

        }
    }
}